package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //1、保存套餐的基本信息————操作setmeal，执行insert操作
        this.save(setmealDto);

        //2、获得套餐菜品关系信息，并遍历设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        //2、批量保存套餐菜品的关系信息————操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1;
        //1、查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0){
            //如果不能删除，抛出一个自定义的业务异常
            throw new CustomException("套餐正在售卖状态，不能删除！");
        }

        //2、如果可以删除，先删除套餐中的数据————setmeal
        this.removeByIds(ids);

        //3、删除关系表中的数据————setmeal_dish
        //delete from setmeal_dish where setmeal_id in (1,2,3);
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(queryWrapper1);
    }

    /**
     * 修改套餐信息时，数据回显
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getData(Long id) {
        //1。根据id查到对应的套餐信息
        Setmeal setmeal = this.getById(id);

        //2。新建一个dto，把setmeal信息复制到setmealDto中
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //3。根据id查询对应的套餐菜品关系信息，并设置给setmealDto
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    /**
     * 修改套餐信息并保存到数据库
     * 需求分析：
     * 同时修改两张表：套餐表和套餐菜品关系表.
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateSetmealDto(SetmealDto setmealDto) {
        //1。从setmealDto拿到套餐的id和套餐菜品关系信息
        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //步骤2可以忽略，因为前端选择菜品时无法选择停售的菜品
        /*
        //2。查看套餐中是否有菜品在停售状态
        //2.1。从套餐菜品关系信息中拿到所有菜品的id
        List<Long> dishIdList = new ArrayList<>();
        for (SetmealDish setmealDish : setmealDishes) {
            dishIdList.add(setmealDish.getDishId());
        }
        //2.2。根据菜品id查询对应的菜品
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(Dish::getId, dishIdList);
        List<Dish> list = dishService.list(queryWrapper1);
        //2.3。查看菜品的状态是否是停售状态
        for (Dish dish : list) {
            if (dish.getStatus() == 0){
                throw new CustomException("套餐有菜品存在停售状态！");
            }
        }
        */

        //3。根据套餐id删除所有的套餐菜品关系信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(queryWrapper);

        //4。前端传过来的套餐菜品关系信息中没有套餐id，设置进去
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }

        //5。保存套餐菜品关系信息，并保存修改后的套餐信息
        setmealDishService.saveBatch(setmealDishes);
        this.updateById(setmealDto);
    }

}
